﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AdbRegisterTool
{
    public static class CmdHelper
    {
        public static string ExecuteCommand(string command, string command1,string path)
        {
            string result = "";
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true,
                WorkingDirectory = path
            };
           
            using (Process process = new Process())
            {
                process.StartInfo = startInfo;
                process.OutputDataReceived += (s, e) => result += e.Data; // 处理命令执行结果

                process.Start();
                process.BeginOutputReadLine();


                if (!string.IsNullOrEmpty(command))
                {
                    process.StandardInput.WriteLine(command); // 执行命令
                    Thread.Sleep(250);
                }
                if (!string.IsNullOrEmpty(command1))
                {
                    process.StandardInput.WriteLine(command1); // 执行命令
                    Thread.Sleep(250);
                }

                process.StandardInput.WriteLine("exit"); // 退出命令提示符
                

                process.WaitForExit();
               
            }

            return result;
        }

        public static string ExecuteCommand(string command, string path)
        {
            string result = "";
            ProcessStartInfo startInfo = new ProcessStartInfo
            {
                FileName = "cmd.exe",
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true,
                WorkingDirectory = path
            };
            Thread.Sleep(5);
            using (Process process = new Process())
            {
                process.StartInfo = startInfo;
                process.OutputDataReceived += (s, e) =>
                {
                    if (!string.IsNullOrEmpty(e.Data))
                    {
                        result += e.Data;  // 拼接输出结果
                        Thread.Sleep(5);
                    }
                };
                process.Start();
                
                process.BeginOutputReadLine();
                

                if (!string.IsNullOrEmpty(command))
                {
                    process.StandardInput.WriteLine(command); // 执行命令
                    Thread.Sleep(500);
                }

                Thread.Sleep(500);
                process.StandardInput.WriteLine("exit"); // 退出命令提示符
               

            

            }

            return result;
        }


        public static async Task<string> ExecuteCommandAsync(string command, string workingDirectory)
        {
            var result = new StringBuilder();


            using (var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    Arguments = "/c " + ConvertToUTF8(command), // 使用/c参数执行命令后退出  
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    WorkingDirectory = workingDirectory
                },
                EnableRaisingEvents = true
            })
            {
                process.OutputDataReceived += (sender, e) =>
                {
                    if (!string.IsNullOrEmpty(e.Data))
                    {
                        result.AppendLine(e.Data);
                    }
                };

                process.ErrorDataReceived += (sender, e) =>
                {
                    if (!string.IsNullOrEmpty(e.Data))
                    {
                        result.AppendLine("Error: " + e.Data);
                    }
                };

                process.Start();

                process.BeginOutputReadLine();
                process.BeginErrorReadLine();

                 process.WaitForExit(8000);

            }

            return result.ToString();
        }


       
    



    public static string ConvertToUTF8(string gb2312String)
        {
            try
            {
                byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(gb2312String);
                return Encoding.UTF8.GetString(bytes);
            }
            catch
            {
                return gb2312String;
            }
        }
    }



}
